19. Exercise: Create a Worker
L9 40 Creating Work SC
Update Note:
In recent work version upgrade, 1.0.0-alpha12 and onwards have a breaking change. ThedoWork()function now returns Result instead of Payload because they have combined Payload into Result.
Result is now a "sealed class" with three concrete implementations -Result.success(),Result.retry(), andResult.failure(). Read more here.
Now it’s your turn to complete this exercise yourself.
You're going to upgrade DevByteViewer to pre-fetch data when the app is in the background. You should use the WorkManager library to accomplish this.
After you’ve completed this exercise and the next one, your users will get the latest data every time they open the app. WorkManager will make sure to schedule the work so it has the lowest impact on battery life possible.
1. See the WorkManager dependency:
Open up app/build.gradle and take a look at the work-runtime dependency.
implementation "android.arch.work:work-runtime-ktx:$version_work"
The library provides all the WorkManager classes you need to complete the final two exercises.
2. Create a class called RefreshDataWorker that is a CoroutineWorker:
In work/RefreshDataWorker.kt, add the RefreshDataWorker class. WorkManager workers always extend a Worker class. We're going to use a CoroutineWorker, because we want to use coroutines to handle our asynchronous code and threading. Have RefreshDataWorker extend from the CoroutineWorker class. You also need to pass a Context and WorkerParameters to the class and its parent class.
class RefreshDataWorker(appContext: Context, params: WorkerParameters):
CoroutineWorker(appContext, params) { }
3. Define your work in the doWork method:
Override the required doWork() method. This is what "work" your RefreshDataWorker does, in our case, syncing with the network. Add variables for the database and the repository.
override suspend fun doWork(): Result {
val database = getDatabase(applicationContext)
val repository = VideosRepository(database)
}
4. Correctly return success or retry:
Use refresh the videos and call Payload() to return SUCCESS or RETRY.
// The doWork() function now returns Result instead of Payload because they have combined Payload into Result.
// Read more here - https://developer.android.com/jetpack/androidx/releases/work#1.0.0-alpha12
override suspend fun doWork(): Result {
val database = getDatabase(applicationContext)
val repository = VideosRepository(database)
return try {
repository.refreshVideos()
Result.success()
} catch (e: HttpException) {
Result.retry()
}
}
If you want to start at this step, you can download this exercise code from: Step.07-Exercise-Create-a-Worker.
You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck go back and watch the video again.
Once you’re done, you can check your solution against the solution we’ve provided here Step.07-Solution-Create-a-Worker or using this git diff
Task Description:
Check the steps below as you implement them to complete this exercise
Task Feedback:
This is great! You’re almost done implementing pre-fetching. In the next section you’ll learn how to schedule work with WorkManager.
Reference documentation